<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<script type="text/javascript">
	var staffDataGrid = $('#staff-datagrid-json').datagrid({
	    gridTitle : '员工管理',
	    showToolbar: true,
	    toolbarItem: '',
	    showLinenumber: false,
	    filterThead: false,
	    filterAll: true,
	    sortAll : true,
	    dataUrl: 'staff/list.do',
	    dataType: 'json',
	    columns: [
	    	{
				name: '',
				label: '操作',
				align: 'center',
				width: 130,
				quicksort:false,
				menu: false,
				edit: false,
				render: function(){
			        var html = '';
			    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue btn-sm" onclick="editStaff(this)"><i class="fa fa-edit"></i> 编辑</button></div>';
				    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-red btn-sm" onclick="delStaff(this)"><i class="fa fa-remove"></i> 删除</button></div>';
				    return html;
				}
	        },
	        {
	            name: 'staffNo',
	            label: '员工编号',
	            align: 'center',
	            width: 100
	        },
	        {
	            name: 'name',
	            label: '员工姓名',
	            align: 'center',
	            width: 100
	        },
	        {
	            name: 'loginName',
	            label: '登录账号',
	            align: 'center',
	            width: 100,
	            render: function(val){
		            if (val == 'null') {
						return "";
					}
					return val;
	            }
	        },
	        {
	            name: 'sex',
	            label: '性别',
	            type : 'select',
	            items: [{'M':'男'}, {'F':'女'}],
	            align: 'center',
	            width: 60,
	            render: $.datagrid.renderItem
	        },
	        {
	            name: 'email',
	            label: '邮箱',
	            align: 'center',
	            width: 180
	        },
	        {
	            name: 'address',
	            label: '地址',
	            align: 'center',
	            width: 200,
	            hide: true
	        },
	        {
	            name: 'phoneNo',
	            label: '手机号',
	            align: 'center',
	            width: 180,
	            hide: false
	        },
	        {
	            name: 'highestEducation',
	            label: '最高学历',
	            type : 'select',
	            items: [{'1':'博士后'}, {'2':'博士'}, {'3':'硕士'}, {'4':'本科'}, {'5':'专科'}, {'6':'高中'}, {'7':'初中以下'}],
	            align: 'center',
	            width: 100,
	            render: $.datagrid.renderItem,
	            hide: true
	        },
	        {
	            name: 'graduatedFrom',
	            label: '毕业院校',
	            align: 'center',
	            width: 200,
	            rule: 'length[~100]',
	            hide: true
	        },
	        {
	            name: 'profession',
	            label: '专业',
	            align: 'center',
	            width: 180,
	            rule: 'length[~32]',
	            hide: true
	        },
	        {
	            name: 'departmentName',
	            label: '所属部门',
	            align: 'center',
	            width: 160,
	            render: function(value){
		            	return value;
		            },
	            hide: false
	        },
	        {
	            name: 'credentialNo',
	            label: '证件号码',
	            align: 'center',
	            width: 140,
	            rule: 'length[~64]',
	            hide: true
	        },
	        {
	        	name: 'positionName',
	            label: '职位',
	            type : 'lookup',
	            attrs: {'data-url':'staff/lookupPosition.do'},
	            align: 'center',
	            width: 100,
	            render: function(value) {
		            return value;
		        }
	        },
	        {
	            name: 'userStatus',
	            label: '员工状态',
	            type : 'select',
	            items: [{'0':'试用'}, {'1':'正式'}, {'2':'离职'}],
	            align: 'center',
	            width: 80,
	            render: $.datagrid.renderItem
	        }
	    ],
	    afterSave: function($trs, datas) {
	        	this.refresh();
	        },
	    paging: {pageSize:20, selectPageSize:'20,30,40'},
	    showTfoot: true,
	    fullGrid: false,
	    toolbarCustom: function(){
	    	var html = '';
	    	html += '<div class="btn-group" role="group"><button type="button" class="btn btn-blue" onclick="addStaff()"><i class="fa fa-plus"></i> 添加</button></div>';
		    html += '<div class="btn-group" role="group"><button type="button" class="btn btn-green" onclick="refreshStaff()"><i class="fa fa-refresh"></i> 刷新</button></div>';
		    return html;
	    }
	});

	try {staffDataGrid.data('bjui.datagrid').colLock(0, true);} catch (e) {}
	
	function refreshStaff() {
		staffDataGrid.datagrid("refresh");
	}

	var staffIndex=-1;
	function addStaff() {
		staffIndex = -1;
		$('#edit-staff-dialog').dialog({
			id:'edit-staff-dialog', 
			url:'staff/add.do', 
			maxable: false,
			mask: true,
			width: 850,
			height: 500,
			onLoad: function($dialog){
				setEditStaffBtnEvent($dialog, true);
			},
			title:'用户管理 - 添加'});
	}
	function editStaff(_this) {
		staffIndex = $(_this).closest("tr").index();
		var data = staffDataGrid.data('bjui.datagrid').getRowData(staffIndex);
		$('#edit-staff-dialog').dialog({
			id:'edit-staff-dialog',
			url:'staff/edit.do?staffNo='+data.staffNo,
			maxable: false,
			mask: true,
			width: 850,
			height: 500,
			onLoad: function($dialog){
				setEditStaffBtnEvent($dialog, false);
				getJsonData2Form('staff/getStaff.do', 'staffSn='+data.staffSn, $('#staff_form'), staffCallback);
			},
			
			title:'用户管理 - 编辑'});
	}
	function delStaff(_btn){
		staffIndex = $(_btn).closest("tr").index();
		var rowData = staffDataGrid.data('bjui.datagrid').getRowData(staffIndex);

		staffDataGrid.alertmsg('confirm', '确定删除当前数据？', 
			{
			    displayMode:'slide', 
	            okName:'确定', 
	            cancelName:'取消', 
	            title:'提示信息',
	            okCall : function(){
	            	var url = "staff/del.do?staffSn="+rowData.staffSn+"&staffNo="+rowData.staffNo;
	    			var data = null;
	    			var successCallBack = function(result){
	    				refreshStaff();
	    			}
	    			var failCallBack = null;
	    			var errorCallBack = null;
	    			myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
	            }
	    });
	}
	 function staffCallback($inputObj, _name, _val){
		$("#l_staffNo").attr("readonly","readonly");
	}
	
	function setEditStaffBtnEvent($dialog, isAdd) {
		var $prev, $next, $save, $cancel;
		$save = $dialog.find('.save');
		$cancel = $dialog.find('.cancel');
	
		$save.click(function(){
			$('#staff_form').isValid(function(v) {
				if(v){
					var url = 'staff/save.do';
	    			var data = $('#staff_form').serialize()+"&addFlag="+isAdd;
	    			var successCallBack = function(result){
	    				$dialog.dialog('closeCurrent');
						staffDataGrid.datagrid('refresh');
	    			}
	    			var failCallBack = null;
	    			var errorCallBack = null;
	    			myAjax(url, data, true, successCallBack, failCallBack, errorCallBack);
				}
			});
		});
	
		$cancel.click(function(){
			$dialog.dialog('closeCurrent');
		});
	}
</script>
<div class="bjui-pageContent" id="staff-index-pagecontent">
    <div style="padding:0px; height:100%; width:100%;">
        <table id="staff-datagrid-json" data-width="100%" data-height="100%" class="table table-bordered">
        </table>
    </div>
</div>
<div id="edit-staff-dialog" style="display: none;">
</div>
<div id="set-password-dialog" style="display: none;">
</div>